
var url = "http://127.0.0.1:6001/"


var stockCfg={
    code:'000001.SZ',
    start_date:'20200101',
    end_date:'20200508',
}

function getVersion(cb){

	$.post('/version',null,function(data){
	  if(data)
	  {
		cb(data)
	  }
	});
}

function getStock(stockId,cb)
{
	var stock = JSON.stringify(stockCfg)
	// stock = null
	// console.log(stock)
	$.get('/quant?s='+stock,function(data){
	  if(data)
	  {
		cb(data)
	  }
	});

}

/*
	fastify 不支持 application/x-www-form-urlencoded 类型
*/
function getStock2(stockId,cb)
{
	var stock = JSON.stringify(stockCfg)
    $.ajax({
        type: "POST",
        url: "quant2",
        data:stock,
        beforeSend: function(request) {
            request.setRequestHeader("Content-Type", "application/json");
        },
        success: function(result) {
            // result = JSON.parse(result);
            cb(result)
        }
    });
}

function getStock3(stockId,cb)
{	
	var stock = JSON.stringify(stockCfg)
    $.ajax({
        type: "POST",
        url: "quant2",
        data:stock,
        // async: true,
        beforeSend: function(request) {
            request.setRequestHeader("Content-Type", "application/json");
        },
        success: function(result) {
            // result = JSON.parse(result);
            cb(result)
            // return result
        },
        failure:function(result){
        	cb(null)
        	// return null
        }
    });
}

var getStockAsync = function(stockId) {
  return new Promise(resolve => {
  	getStock3(stockId,function(data){
    	resolve(data);
    	init(data);
  	})
  });
};

function update(){

}


function init(data) {

	// var code = '000001.SZ'
	// getStock3(code,function(data){
    // create the chart
    Highcharts.stockChart('container1', {
      rangeSelector: {
        selected: 1
      },

      title: {
        text: stockCfg.code
      },

      series: [{
        type: 'candlestick',
        name: stockCfg.code,
        data: data,
        // dataGrouping: {
        //   units: [
        //     [
        //       'week', // unit name
        //       [1] // allowed multiples
        //     ], [
        //       'month',
        //       [1, 2, 3, 4, 6]
        //     ]
        //   ]
        // }
      }]
    });
  // })
}

var tv = 60*1000
setInterval(update,tv)